package com.mach.platform.service;

import com.google.common.collect.Lists;
import com.mach.platform.domain.DMFConcentrationRecord;
import com.mach.platform.domain.PitcherAutonomy;
import com.mach.platform.domain.ProductionState;
import com.mach.platform.repository.DMFConcentrationRecordRepo;
import com.mach.platform.utils.springdata.DynamicSpecifications;
import com.mach.platform.utils.springdata.SearchFilter;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;

@Service
public class DMFConcentrationRecordServiceImpl implements BaseService<DMFConcentrationRecord> {

    @Autowired
    private DMFConcentrationRecordRepo dmfConcentrationRecordRepo;
    private static final DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    @Override
    public DMFConcentrationRecord save(DMFConcentrationRecord dmfConcentrationRecord) {
        return null;
    }

    @Override
    public List<DMFConcentrationRecord> insertObjInBatch(List<DMFConcentrationRecord> list) {
        return null;
    }

    @Override
    public DMFConcentrationRecord update(DMFConcentrationRecord dmfConcentrationRecord) {
        return null;
    }

    @Override
    public int delete(Long[] ids) {
        return dmfConcentrationRecordRepo.deleteByIdIn(ids);
    }

    @Override
    public Page<DMFConcentrationRecord> list(Pageable pageable, Map params) {
        if (params.size() > 0){
            Object startTime = params.get("startTime");
            Object endTime = params.get("endTime");
            if (startTime != null && startTime != "" && endTime != null && endTime != "") {
                LocalDateTime ldtStartTime = LocalDateTime.parse(startTime.toString(), df);
                LocalDateTime ldtEndTime = LocalDateTime.parse(endTime.toString(), df);
                Page<DMFConcentrationRecord> result = dmfConcentrationRecordRepo.getListByTime(ldtStartTime, ldtEndTime, pageable);
                return result;
            }
        }
        return null;
    }

    @Override
    public DMFConcentrationRecord info(Long id) {
        return null;
    }

    public void add(List<DMFConcentrationRecord> dmfConcentrationRecords) {
        if (CollectionUtils.isNotEmpty(dmfConcentrationRecords)){
            List<DMFConcentrationRecord> newList = Lists.newArrayList();

            dmfConcentrationRecords.forEach(dmfConcentrationRecord -> {
                if (StringUtils.isNotBlank(dmfConcentrationRecord.getCustomNumber())){
                    if (dmfConcentrationRecord.getDMFConcentrationRecordTime() == null){
                        dmfConcentrationRecord.setDMFConcentrationRecordTime(LocalDateTime.now());
                    }
                    newList.add(dmfConcentrationRecord);
                }
            });

            //补全课长、值班主管
            String sectionChief = null;
            String shiftSupervisor = null;
            for (DMFConcentrationRecord dmfConcentrationRecord : dmfConcentrationRecords){
                if (StringUtils.isNotBlank(dmfConcentrationRecord.getSectionChief())){
                    sectionChief = dmfConcentrationRecord.getSectionChief();
                }
                if (StringUtils.isNotBlank(dmfConcentrationRecord.getShiftSupervisor())){
                    shiftSupervisor = dmfConcentrationRecord.getShiftSupervisor();
                }
            }
            for (DMFConcentrationRecord dmfConcentrationRecord : dmfConcentrationRecords){
                dmfConcentrationRecord.setSectionChief(sectionChief);
                dmfConcentrationRecord.setShiftSupervisor(shiftSupervisor);
            }

            dmfConcentrationRecordRepo.saveAll(newList);
        }
    }

    public void update(List<DMFConcentrationRecord> dmfConcentrationRecords) {
        this.add(dmfConcentrationRecords);
    }

    public List<DMFConcentrationRecord> getProductionStateByTime(String time) {
        if (time != null && !time.equals("")){
            List<DMFConcentrationRecord> result = dmfConcentrationRecordRepo.getProductionStateByTime(time);
            return result;
        }
        return null;
    }
}
